package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 智能设备Controller
 *
 * @author ruoyi
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "智能设备的接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备列表")
    public TableDataInfo<Device> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 从物联网平台同步产品列表
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }

    /**
     * 添加设备
     */
    @PostMapping("/register")
    @ApiOperation(value = "添加设备")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 查询设备详情
     */
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> detail(@PathVariable String iotId) {
        DeviceDetailVo deviceDetailVo = deviceService.queryDeviceDetail(iotId);
        return R.ok(deviceDetailVo);
    }

    /**
     * 修改设备
     */
    @PutMapping
    @ApiOperation(value = "修改设备")
    public AjaxResult update(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return success();
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation(value = "删除设备")
    public AjaxResult remove(@PathVariable String iotId) {
        deviceService.removeDevice(iotId);
        return success();
    }

    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        return deviceService.queryServiceProperties(iotId);
    }

    /**
     * 查询产品详情
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("查询产品详情")
    public AjaxResult queryProduct(@PathVariable String productKey) {
        return deviceService.queryProduct(productKey);
    }
}